﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using SupplyChain.Domain.Entities;
using SupplyChain.ErrorCode;
using SupplyChain.Write.API.Application.Command.Goods;
using SupplyChain.Write.API.Application.Command.PurchaseOrder;

namespace SupplyChain.Write.API.Controllers
{

    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PurchaseOrderController : ControllerBase
    {
        private readonly IMediator mediator;

        public PurchaseOrderController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取采购订单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<PurchaseOrder>>> GetAllPOList()
        {
            return ApiResponse<List<PurchaseOrder>>.Success(await mediator.Send(new GetPurchaseOrderListCommand()));
        }

        /// <summary>
        /// 创建采购订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> AddPurchaseOrder(AddPurchaseOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改采购订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResponse<bool>> UpdatePurchaseOrder(UpdPurchaseOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 删除采购订单
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResponse<bool>> DeletePurchaseOrder([FromQuery] DelPurchaseOrderCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 获取采购类型列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResponse<List<PurchaseCategory>>> GetPurchaseCategory()
        {
            return ApiResponse<List<PurchaseCategory>>.Success(await mediator.Send(new GetPurchaseCategoryCommand()));
        }

        /// <summary>
        /// 新增采购类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResponse<bool>> AddPurchaseCategory(AddPurchaseCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 修改采购类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResponse<bool>> UpdatePurchaseCategory(UpdPurchaseCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }

        /// <summary>
        /// 删除采购类型
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResponse<bool>> DeletePurchaseCategory([FromQuery] DelPurchaseCategoryCommand command)
        {
            bool result = await mediator.Send(command);
            if (result)
            {
                return ApiResponse<bool>.Success(result);
            }
            return ApiResponse<bool>.Error(400, "操作失败");
        }
    }
}
